package com.fh.fruit.service.version.impl;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.fruit.entity.version.AppVersion;
import com.fh.fruit.service.version.VersionManager;
import com.fh.fruit.util.OSSUtils;
import com.fh.util.PageData;
import com.fh.util.TwoDimensionCode;

@Service("appVersionService")
public class VersionService implements VersionManager {
	@Resource(name = "daoSupport")
	private DaoSupport dao;

	 @Value("${oss.url}")
	 private String ossUrl;

	@Override
	public List<PageData> appVersionlistPage(Page page) throws Exception {
		@SuppressWarnings("unchecked")
		List<PageData> list = (List<PageData>) dao.findForList("AppVsersionMapper.appVersionlistPage", page);
		return list;
	}

	@Override
	public void saveAppVersion(AppVersion av, MultipartFile pic) throws Exception {
		String content = null;
		String upload = av.getUpload1();
		String apkUrl =null;
		if (av != null) {
			if ("iOS".equals(av.getType())) {
				dao.save("AppVsersionMapper.saveAppVersion", av);
			} else {
				String au = av.getApkUrl();
				if ("01".equals(upload)) {
					apkUrl = ossUrl + "/fruit/apk/" + av.getType() + "/" + av.getName() + "/fruit.apk";
				}else{
					apkUrl=av.getApkUrl();
				}
				String qcodeUrl = ossUrl + "/fruit/apk/" + av.getType() + "/" + av.getName() + "/fruit.png";
				av.setApkUrl(apkUrl);
				av.setQcodeUrl(qcodeUrl);
				int num = (int) dao.save("AppVsersionMapper.saveAppVersion", av);
				if (num == 1 && !"iOS".equals(av.getType())) {
					if ("01".equals(upload)) {
						content = apkUrl;
						String fileName = "fruit/apk/" + av.getType() + "/" + av.getName() + "/fruit.apk";
						Map<String, Object> uploadPicture = OSSUtils.pictureUpload(fileName, pic,
								"application/octet-stream");
						int error = (int) uploadPicture.get("error");
						if (error != 0) {
							TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						}
					} else {
						content = au;
					}
					InputStream is = TwoDimensionCode.encodeZXcode(content, "png");
					String fileName = "fruit/apk/" + av.getType() + "/" + av.getName() + "/fruit.png";
					Map<String, Object> resMap = OSSUtils.pictureUploadI(fileName, is);
					int error = (int) resMap.get("error");
					if (error != 0) {
						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					}
				}
			}
		}
	}

	@Override
	public void editAppVersion(AppVersion av, MultipartFile pic) throws Exception {
		if (av != null) {
			if ("iOS".equals(av.getType())) {
				dao.update("AppVsersionMapper.editAppVersion", av);
			} else {
				String upload = av.getUpload1();
				String content = null;
				String apkUrl =null;
				String au = av.getApkUrl();
				if ("01".equals(upload)) {
					apkUrl = ossUrl + "/fruit/apk/" + av.getType() + "/" + av.getName() + "/fruit.apk";
				}else{
					apkUrl=av.getApkUrl();
				}
				String qcodeUrl = ossUrl + "/fruit/apk/" + av.getType() + "/" + av.getName() + "/fruit.png";
				av.setApkUrl(apkUrl);
				av.setQcodeUrl(qcodeUrl);
				int num = (int) dao.update("AppVsersionMapper.editAppVersion", av);
				if (num == 1) {
					if ("01".equals(upload)) {
						content = apkUrl;
						String fileName = "fruit/apk/" + av.getType() + "/" + av.getName() + "/fruit.apk";
						Map<String, Object> uploadPicture = OSSUtils.pictureUpload(fileName, pic, "application/octet-stream");
						int error = (int) uploadPicture.get("error");
						if (error != 0) {
							TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						} else {
							
						}
					} else {
						content = au;
					}
					InputStream is = TwoDimensionCode.encodeZXcode(content, "png");

					String fileName = "fruit/apk/" + av.getType() + "/" + av.getName() + "/fruit.png";
					Map<String, Object> resMap = OSSUtils.pictureUploadI(fileName, is);
					int error = (int) resMap.get("error");
					if (error != 0) {
						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					}
				}
			}
		}
	}

	@Override
	public PageData findById(PageData pd) throws Exception {
		return (PageData) dao.findForObject("AppVsersionMapper.findById", pd);
	}

	@Override
	public void deleteAppVersion(PageData pd) throws Exception {
		dao.delete("AppVsersionMapper.deleteAppVersion", pd);
	}

	@Override
	public int findByCode(PageData pd) throws Exception {
		// TODO Auto-generated method stub
		return (int) dao.findForObject("AppVsersionMapper.findByCode", pd);
	}

}
